package com.mine.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mine.pojo.*;
import com.mine.service.CartService;
import com.mine.service.MealService;
import com.mine.service.OrderService;
import com.mine.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    MealService mealService;
    @Autowired
    CartService cartService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    //查看所有菜品
    @GetMapping("mealList")
    public String mealList(String class0, Model model, @RequestParam(defaultValue = "1") Integer pageNum, String mm) {
        if (class0 == null || "".equals(class0)) {
            class0 = "0";
        }
        if (mm != null) {
            model.addAttribute("mm", "mm");
        }
        //分页，每页7个
        PageHelper.startPage(pageNum, 5);
        List<Meal> mealList = mealService.shopList(class0, "0", null);
        PageInfo<Meal> pageInfo = new PageInfo<>(mealList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("mealList", mealList);
        model.addAttribute("class0", class0);
        return "admin/meal/admin_mealList";
    }

    //查找某一个菜品的信息（修改）
    @GetMapping("adminqueryMealOne")
    public String adminqueryMealOne(String mm, String meal_id, Model model) {
        Meal meal = mealService.getMealById(Integer.valueOf(meal_id));
        model.addAttribute("meal", meal);
        if (mm != null) {
            model.addAttribute("mm", "mm");
        }
        return "admin/meal/admin_meal_update";
    }

    //更新菜品
    @PostMapping("updateMealInformation")
    public String updateMealInformation(HttpSession session, MultipartFile file, Meal meal) throws IOException {
        mealService.updateMealInformation(session, file, meal);
        return "redirect:/admin/adminqueryMealOne?mm=0&meal_id=" + meal.getMeal_id();
    }

    //添加菜品
    @GetMapping("addmealList")
    public String addmealList() {
        return "admin/meal/admin_meal_add";
    }

    @PostMapping("addmealList")
    public String addmealList1(HttpSession session, MultipartFile file, Meal meal) throws IOException {
        System.out.println(meal);
        mealService.addMealInformation(session, file, meal);
        return "redirect:/admin/mealList?mm=0";
    }

    //删除菜品
    @ResponseBody
    @GetMapping("remove_meal")
    public String remove_meal(String meal_id) {
        mealService.remove_meal(meal_id);
        return "OK";
    }

    //管理员查看评论
    @GetMapping("adminqueryMealcart")
    public String adminqueryMealcart(String meal_id, Model model) {
        List<Cart> cartList = cartService.adminqueryMealcart(meal_id);
        model.addAttribute("cartList", cartList);
        return "admin/meal/admin_meal_cart";
    }

    //查看订单
    @GetMapping("adminOrderList")
    public String adminOrderList(@RequestParam(defaultValue = "1") String order_state, Model model, @RequestParam(defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, 7);
        List<Order> orderList = orderService.adminOrderList(order_state);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("orderList",orderList);
        model.addAttribute("order_state",order_state);
        return "admin/orders/admin_ordersList";
    }

    //*查看订单
    @RequestMapping("adminLookOrder")
    public String adminLookOrder(String order_id, Model model){
        Order order = orderService.queryOrderById(order_id);
        List<OrderItem> orderItemList = orderService.queryAllOrderItemByOrder(order_id);
        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("order",order);
        return "admin/orders/admin_orderOtemlist";
    }

    //查看全部用户
    @GetMapping("adminUserList")
    public String adminUserList(Model model, @RequestParam(defaultValue = "1") Integer pageNum){
        PageHelper.startPage(pageNum, 7);
        List<User> userList = userService.queryAllUser();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("userList",userList);
        return "admin/user/admin_UserList";
    }
    //发货
    @GetMapping("adminshopstate")
    @ResponseBody
    public String adminshopstate(String order_id){
        orderService.updateOrdershopstate(order_id);
        return "OK";
    }
    //签收
    @GetMapping("adminshopstate1")
    @ResponseBody
    public String adminshopstate1(String order_id){
        orderService.updateOrdershopstate1(order_id);
        return "OK";
    }
}
